# Contributor: Mathias LANG <pro.mathias.lang@gmail.com>
# Maintainer: Mathias LANG <pro.mathias.lang@gmail.com>
pkgname=dmd
pkgver=2.091.1
pkgrel=0
pkgdesc="D Programming Language reference compiler"
url="https://github.com/dlang/dmd"
# TODO: Enable on x86 once LDC-1.20.1 is out (and enabled on x86)
arch="x86_64"
license="BSL-1.0"
depends="libexecinfo-dev tzdata"
makedepends="ldc"
checkdepends="bash diffutils gdb grep"
source="dmd-$pkgver.tar.gz::https://github.com/dlang/dmd/archive/v$pkgver.tar.gz
		druntime-$pkgver.tar.gz::https://github.com/dlang/druntime/archive/v$pkgver.tar.gz
		phobos-$pkgver.tar.gz::https://github.com/dlang/phobos/archive/v$pkgver.tar.gz

		dmd-install-config.conf
		10-dmd-musl.patch
		20-dmd-remove-tests.patch
		"

prepare() {
	# The Makefiles make some assumption about the directory structure
	ln -s "$srcdir/dmd-$pkgver/" "$srcdir/dmd"
	ln -s "$srcdir/druntime-$pkgver/" "$srcdir/druntime"
	ln -s "$srcdir/phobos-$pkgver/" "$srcdir/phobos"

	builddir="$srcdir"
	default_prepare
}

build() {
	# Compile with the host compiler
	make -C "$srcdir/dmd/" -f posix.mak HOST_DMD=ldmd2 ENABLE_RELEASE=1 INSTALL_DIR="$srcdir/install" install
	make -C "$srcdir/druntime/" -f posix.mak ENABLE_RELEASE=1 INSTALL_DIR="$srcdir/install" install
	make -C "$srcdir/phobos/" -f posix.mak ENABLE_RELEASE=1 INSTALL_DIR="$srcdir/install" install
}

check() {
	make -C "$srcdir/dmd-$pkgver/" HOST_DMD=ldmd2 -f posix.mak test
# TODO: This currently does not pass - Some Druntime work needed
#	make -C "$srcdir/druntime-$pkgver/" -f posix.mak unittest
#	make -C "$srcdir/phobos-$pkgver/" -f posix.mak unittest
}

package() {
	mkdir -p "$pkgdir/usr/bin/" "$pkgdir/usr/lib/" "$pkgdir/etc/" "$pkgdir/usr/include/dmd/"

	mv "$srcdir"/install/linux/bin64/dmd "$pkgdir"/usr/bin/dmd
	cp "$srcdir"/dmd-install-config.conf "$pkgdir"/etc/dmd.conf
	mv "$srcdir"/install/linux/lib64/* "$pkgdir"/usr/lib/

	mv "$srcdir"/install/src/druntime/import/ "$pkgdir"/usr/include/dmd/druntime
	mv "$srcdir"/install/src/phobos/ "$pkgdir"/usr/include/dmd/phobos/

	chmod +x "$pkgdir"/usr/bin/dmd
}

sha512sums="d15035fe7e9d086388348b4123586c98a9f3c7f881ce5c43cfcab78cfcf7853c4f29ed70a66009fb614a220dbe2bf36c7a0c5df0901c513edb9a5f2f6f6ae51e  dmd-2.091.1.tar.gz
54f1f802f7912bdb25c0878fc5fc640b0ce34d3319cc7967455fd20df5a76be3f5a0261121a760bf3e11ad99c0dcde07755988e48c5cbf1ffb344ede0d88903c  druntime-2.091.1.tar.gz
edefea6cc02812c3abdc24b195ed8dd95e38863b617793ea8ce75c83c5b2d1274f210a10613456f4207f0038dcb5abd81fdfe0ff30d9204e5df4ead9d3ffcd37  phobos-2.091.1.tar.gz
123ec0f256a73030a5e5b4b87a7f2e0752320777b7fcd175a221807ec2917f5d6d88776c3448eab077eb7a2211dd4a3d64e3a556053b0f183eb058da437bc5da  dmd-install-config.conf
a41784325801f837a333cf4e550025f1f354de690a520529f12cba5e3b7ceddc63e8a9e9c3d06f858f421cfc8209f33eb0be967d39a75011cb07cfd56ac84177  10-dmd-musl.patch
e507dad7427a3386a851b4a6c79130253fd59ddac6a460f61f21d0af0e608674ccfe8c9d2ca8b906f1f519210698f9bdf5b4af24d4050112f8ee6f9e8bb2f274  20-dmd-remove-tests.patch"
